// index/pages/pastYear/pastYear.js
Page({
  data: {
    totalQuestions: 500,
    totalExams: 30,
    correctAnswers: 10,
    showExamDialog: false,
    currentExam: null,
    examList: [
      {
        id: 1,
        name: '城市规划原理2024年考试习题...',
        questionCount: 100,
        totalScore: 100,
        passingScore: 60,
        duration: 120,
        status: 'done'
      },
      {
        id: 2,
        name: '生物化学考研题库 生物化学考研题库',
        questionCount: 100,
        totalScore: 100,
        passingScore: 60,
        duration: 120,
        status: 'todo'
      },
      {
        id: 3,
        name: '生物化学考研题库 生物化学考研题库',
        questionCount: 100,
        totalScore: 100,
        passingScore: 60,
        duration: 120,
        status: 'todo'
      }
    ]
  },

  onLoad: function(options) {
    // 可以在这里请求真实数据
    // this.getExamData();
  },

  // 获取考试数据
  getExamData: function() {
    wx.request({
      url: 'https://your-api-url/exam/list',
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            examList: res.data.list,
            totalQuestions: res.data.totalQuestions,
            totalExams: res.data.totalExams,
            correctAnswers: res.data.correctAnswers
          });
        }
      }
    });
  },

  // 开始考试
  startExam: function(e) {
    const examId = e.currentTarget.dataset.id;
    const exam = this.data.examList.find(item => item.id === examId);
    
    this.setData({
      showExamDialog: true,
      currentExam: exam
    });
  },

  // 关闭弹窗
  closeDialog: function() {
    this.setData({
      showExamDialog: false
    });
  },

  // 确认开始考试
  confirmStartExam: function() {
    this.closeDialog();
    const examId = this.data.currentExam.id;
// 跳转到考试页面
    wx.navigateTo({
      url: '/index/pages/exam/exam',
      success: () => {
        console.log("成功跳转到考试页面");
      },
      fail: (err) => {
        console.error("跳转失败:", err);
        wx.showToast({
          title: '跳转失败',
          icon: 'none'
        });
      }
    });
  },

  // 查看模考记录
  viewMockRecord() {
    wx.navigateTo({
      url: '/index/pages/mockRecord/mockRecord'
    });
  },

  // 查看排行榜
  viewRanking() {
    wx.navigateTo({
      url: '/index/pages/rankingList/rankingList'
    });
  },

  // 查看会员
  viewMembership: function() {
    wx.navigateTo({
      url: '/pages/membership/index'
    });
  },

  // 解锁全部
  unlockAll: function() {
    wx.showModal({
      title: '开通会员',
      content: '确定要开通会员解锁全部内容吗？',
      success: (res) => {
        if (res.confirm) {
          // 调用支付接口
          this.requestPayment();
        }
      }
    });
  },

  // 请求支付
  requestPayment: function() {
    wx.requestPayment({
      timeStamp: '',
      nonceStr: '',
      package: '',
      signType: 'MD5',
      paySign: '',
      success: (res) => {
        wx.showToast({
          title: '支付成功',
          icon: 'success'
        });
        // 刷新考试列表状态
        this.updateExamStatus();
      },
      fail: (res) => {
        wx.showToast({
          title: '支付取消',
          icon: 'none'
        });
      }
    });
  },

  // 更新考试状态
  updateExamStatus: function() {
    const updatedList = this.data.examList.map(item => {
      return {...item, status: 'done'};
    });
    
    this.setData({
      examList: updatedList
    });
  }
});